function FlushSitem()
    local scene = g_Data.worldScene;
    if scene then
        for k,v in pairs(CONST.items) do
            if scene[k] then
                scene[k]:setString(g_Player.items[v]);
            end
        end
    end
end

function EnterGates()
    local scene = LoadJson("map.json");
    scene.move = scene.layer:getChildByName("move");
    scene.ui = scene.layer:getChildByName("ui");
    scene.world = scene.move:getChildByName("world");
    scene.layer:setContentSize(scene.world:getContentSize())
    
    AudioEngine.playMusic(EXCEL.sound.world.value, true)
    
    for k,v in pairs(CONST.items) do
        scene[k] = scene.layer:getChildByName(k);
    end
    
    g_Data.worldScene = scene;
    
    FlushSitem();
    RunScene(scene);
    
    local function setReturnButton()
        local function returnCallBack(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                EnterLogin();
            end
        end
        local button = scene.ui:getChildByName("return")
        SetButtonFunc(button, returnCallBack, "clkReturn")
    end
    setReturnButton();
    
    function clickGate(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local mapData = EXCEL.map[sender:getName()];
            if mapData then
                local gateId = mapData.gate;
                local gate = EXCEL.gate[gateId];
                if gate then
                    scene.layer:setEnabled(false)
                    EnterGateInfo(scene, gateId);  
                end
            end
        end
    end
    local childs = scene.move:getChildren();
    for i=1, #childs do
        local name = childs[i]:getName();
        if EXCEL.map[name] then
            SetButtonFunc(childs[i], clickGate, "clkGateInfo")
        end
    end
    
    local mouseEnable = true;
    local preX = -1;
    local function MouseMove(sender, eventType)
        if not mouseEnable then
            return;
        end;
        
        local p = sender:getTouchMovePosition()
        local x = scene.move:getPosition();

        if eventType == ccui.TouchEventType.began then
            preX = p.x;
        elseif eventType == ccui.TouchEventType.moved then
            local move = 0;
            if p.x>preX then
                move = EXCEL.const["mapMoveX"];
            else 
                move = -EXCEL.const["mapMoveX"];
            end
            preX = p.x;
            x = x + move;
            if x>0 then
                x = 0;
            end
            local size = scene.world:getContentSize().width - CONST_GAME_WIDTH;
            if x < -size then
                x = -size;
            end
            scene.move:setPositionX(x);
        elseif eventType == ccui.TouchEventType.ended then
            local move = 0;
            if p.x>preX then
                move = EXCEL.const["mapMoveX"];
            else 
                move = -EXCEL.const["mapMoveX"];
            end
            preX = p.x;
            x = x + move;
            if x>0 then
                x = 0;
            end
            local size = scene.world:getContentSize().width - CONST_GAME_WIDTH;
            if x < -size then
                x = -size;
            end
            scene.move:setPositionX(x);
        end
    end
    scene.layer:addTouchEventListener(MouseMove)
    
    local function AddCallFunc(name, func, sound)
        if not name or not func then
            return;
        end
        local function comCallBack(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                TraverseNode(scene.layer, function(node) return node:getName()=="return" end)
                mouseEnable = false;
                local s,returnButton = func(scene.layer);
                
                local function returnCallBack(sender, eventType)
                    if eventType == ccui.TouchEventType.ended then
                        TraverseNode(scene.layer, function(node) return true end)
                        scene.data.remove[s] = true;
                        mouseEnable = true;
                        setReturnButton()
                    end
                end
  
                local button = scene.ui:getChildByName("return")
                SetButtonFunc(button, returnCallBack, "clkReturn")
                if returnButton then
                    SetButtonFunc(returnButton, returnCallBack, "clkReturn")
                end
            end
        end
        local button = scene.ui:getChildByName(name)
        if button then
            SetButtonFunc(button, comCallBack, sound)
        end
    end
    AddCallFunc("box", EnterBox)
    AddCallFunc("com", EnterCom, "clkEntCom")
    AddCallFunc("pet", EnterPet, "clkEntPet")
    AddCallFunc("equip", EnterEquip, "clkEntEquip")
    AddCallFunc("elite", EnterElite)
    
    
end

